﻿using System.Collections.Generic;

namespace Mp3Observer.Common.Services
{
    /// <summary>
    /// This class provides notification for IObserverAccess instances
    /// </summary>
    public sealed class Observer : Singleton<Observer>
    {
        #region Fields

        /// <summary>
        /// the observers that can get notified
        /// </summary>
        private IList<IObserver> observers;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="Observer"/> class and initializes the observers IList
        /// </summary>
        public Observer()
        {
            this.observers = new List<IObserver>();
        }

        #endregion

        #region IObserverAccess Members

        /// <summary>
        /// Registers the specified observer.
        /// </summary>
        /// <param name="observer">The observer.</param>
        public void Register(IObserver observer)
        {
            if (!this.observers.Contains(observer))
                this.observers.Add(observer);
        }

        /// <summary>
        /// Unregisters the specified observer.
        /// </summary>
        /// <param name="observer">The observer.</param>
        public void Unregister(IObserver observer)
        {
            if (this.observers.Contains(observer))
                this.observers.Remove(observer);
        }

        /// <summary>
        /// Call Notify method for every observer
        /// </summary>
        public void Notify()
        {
            this.observers.ForEach(a => a.Notify());
        }

        /// <summary>
        /// Call Load method for every observer
        /// </summary>
        public void Load()
        {
            this.observers.ForEach(a => a.Load());
        }

        #endregion
    }
}
